<template xmlns:v-action="http://www.w3.org/1999/xhtml">
  <div>
  <a-card :bordered="false">
    <a-button @click="handleGoBack" style="padding-bottom: -10px">返回</a-button>
    <div style="float: right">批次号：{{this.record.couponStockId}}</div>
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="核销状态">
              <a-select v-model="queryParam.state"
                        allowClear
              >
                <a-select-option :key="0">未核销</a-select-option>
                <a-select-option :key="1">已核销</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24">
            <a-form-item label="昵称">
              <a-input v-model="queryParam.nickName" placeholder="请输入"/>
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24">
            <a-form-item label="openid">
              <a-input v-model="queryParam.openid" placeholder="请输入"/>
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24">
            <a-form-item label="起始时间">
              <a-date-picker
                format="YYYY-MM-DD"
                v-model="queryParam.startTime"
                placeholder="开始"
              />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="结束时间">
              <a-date-picker
                format="YYYY-MM-DD"
                placeholder="结束"
                v-model="queryParam.enTime"
              />
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button v-action:query type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button v-action:query style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <span>未核销：<a-tag color="purple">{{statistics.wCount}} </a-tag></span>
      <span>已核销：<a-tag color="red">{{statistics.yCount}} </a-tag></span>
      <!--<span>过期：<a-tag color="red">{{statistics.zCount}} </a-tag></span>-->
    </div>
    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :indentSize="40"
    >

      <span slot="avatarUrl" slot-scope="text,record">
          <template>
            <div class="dfhead">
              <img :src="record.avatarUrl" alt="" style="width: 50px;height: auto;">
            </div>
        </template>
      </span>
      <span slot="integral" slot-scope="text,record">
          <template>
              {{record.status==0?'-':'+'}}{{record.integral}}
        </template>
      </span>

      <span slot="state" slot-scope="text">
        <a-tag v-if="text==0" color="red">
          {{ text | statusFilter }}
        </a-tag>
        <a-tag v-if="text==1" color="blue">
          {{ text | statusFilter }}
        </a-tag>
      </span>

      <span slot="action" slot-scope="text, record">
        <template>
         <a-divider type="vertical"/>
          <a @click="pcPay(record)">核销</a>
        </template>
      </span>
    </s-table>
  </a-card>
  </div>
</template>

<script>
  import STable from '@/components/table/'
  import { selectDetailPage } from '../../../api/memberService/member'
  import { pcPay } from '@/api/productService/businesscoupon'
  import moment from 'moment'

  export default {
    components: {
      STable
    },
    props: {
      record: {
        type: [Object, String],
        default: ''
      }
    },
    //--############  data() ###############-->
    data() {
      return {
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        statistics: {
          wCount: 0,
          yCount: 0,
          zCount: 0
        },
        // 表头
        columns: [
          {
            title: 'id',
            dataIndex: 'id'
          },
          {
            title: '创建时间',
            dataIndex: 'createdTime'
          },
          {
            title: '昵称',
            dataIndex: 'nickName'
          },
          {
            title: '券号',
            dataIndex: 'couponCode'
          },
          {
            title: '团购券名称',
            dataIndex: 'couponName'
          },
          {
            title: '状态',
            dataIndex: 'state',
            scopedSlots: { customRender: 'state' }
          },
          {
            title: 'openid',
            dataIndex: 'openid'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '140px',
            scopedSlots: { customRender: 'action' }
          }

        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          const beginTimeQuery = this.queryParam.startTime //undefined
          const endTimeQuery = this.queryParam.enTime //undefined
          if (beginTimeQuery != undefined) {//点击查询

            this.queryParam.beginTime = moment(beginTimeQuery).format('YYYY-MM-DD') + ' 00:00:00'
          }
          if (beginTimeQuery != undefined) {
            this.queryParam.endTime = moment(endTimeQuery).format('YYYY-MM-DD') + ' 23:59:59'
          }
          this.queryParam.type = 2
          this.queryParam.stockId = this.record.stockId;
          return selectDetailPage(Object.assign(parameter, this.queryParam))
            .then(res => {
              this.statistics = res.result.statistics
              return res.result.pageInfo
            })
        },
        selectedRowKeys: [],
        selectedRows: []
      }
    },
    //--############  data() ###############-->
    //--############  methods() ###############-->
    methods: {
      toggleAdvanced() {
        this.advanced = !this.advanced
      },
      handleOk() {
        // 新增/修改 成功时，重载列表
        this.$refs.table.refresh(true)
      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.mdl = {}
      },
      handleCancel() {
        this.close()
      },
      handleGoBack () {
        this.$emit('onGoBack')
      },
      pcPay(row) {
        const _this = this
        this.$confirm({
          title: '警告',
          content: `真的要核销 ${row.couponCode} 吗?`,
          okText: '确定',
          okType: 'danger',
          cancelText: '取消',
          onOk() {
            // 在这里调用删除接口
            pcPay(row.couponCode).then(res => {
              _this.$message.info(res.message)
              _this.$refs.table.refresh()
            })
          },
          onCancel() {
            console.log('Cancel')
          }
        })
      },
    },
    filters: {
      statusFilter(state) {
        const statusMap = {
          0:'未核销',
          1: '已核销'
        }
        return statusMap[state]
      }
    }
    //--############  methods() ###############-->
  }
</script>
<style scoped>
  .dfhead{
    width: 50px;
    height: 50px;
    background: url("../../../assets/icons/dfhead.png") no-repeat ce nter;
    background-size: 50px;
    border-radius: 25px;
    overflow: hidden;
  }
</style>